﻿package com.cilico.utils;


import android.R.string;
import android.app.Activity;

/** 
 * 数据库相关
 * @author lv.xiangan
 *
 */
public class PackageManagerAndroid {
	/** Define the data type need to be accessed through this class */
	final static int COOKIE = 1;
	public final static int CHANNELS_HISTORY_SIZE = 5;
	/** Keys to classify information in the RMS store. */
	public final static String RMSKEY_COOKIE = "cookie";
	public final static String RMSKEY_LOGINKEY = "loginkey";
	public final static String RMSKEY_USERID = "userid";
	public final static String RMSKEY_LAUNCH_SCREEN = "isShowLS";
	public final static String RMSKEY_ZOOM_LEVEL = "defZoomL";
	public final static String RMSKEY_FIRST_LOGIN = "isFirstLogin";
	public final static String RMSKEY_GATEWAY = "gateway";
	public final static String RMSKEY_LOCATION = "initlocation";
	public final static String RMSKEY_POI = "initpoi";
	public final static String RMSKEY_CHANNELID = "initchannel";
	public final static String RMSKEY_CHANNEL_USERNAME = "channelUser";// for channel
	// action use.
	public final static String RMSKEY_CHANNEL_PASSWORD = "channelPassword";
	public final static String RMSKEY_CHANNELSORT = "chsort";
	public final static String RMSKEY_CURRENTMAP = "map"; // Stores current map ID and
	// name.
	public final static String RMSKEY_CHANNEL_APP = "app_channels";
	public final static String RMSKEY_MESSAGE_LIST = "messages_list";
	public final static String RMSKEY_CHANNEL_REALID = "channeRealId";
	public final static String RMSKEY_LOGODATA = "gogo_data";
	public final static String RMSKEY_QUERY_STRING = "queryString";// current query.
	public final static String PERM_DB = "APP_DB"; // This is historical.
	public final static String MSGSUM_DB = "MESSAGE_LIST";
	public final static String SESSION_DB = "SESSION";
	public final static String POI_DB = "POI";
	public final static String MAP_DB = "MAP_DB";
	public final static String CHANNEL_HISTORY_DB = "CHANNEL_HISTORY";
	public final static String LOGIN_DB = "LOGIN_DB";
	public final static String AUTOTEXT_DB = "AutoText_DB";
	public final static String SAVEPROPERTY_DB = "SAVEPROPERTY_DB";
	/** 本地购物车显示的数据 */
	public final static String SHOPCART_DB = "SHOPPING_CART_DB";
	/** 本地购物车实际的数据 */
	public final static String SHOPCART_REAL_DB = "SHOPCART_REAL_DB";
	/**登陆地址*/
	public final static String LOGIN_URL = "login_url";
	/**当前餐馆*/
	public final static String ID_RESTAURANT = "id_restauRant";
	/**登录用户名*/
	public final static String USER_NAME = "userName";
	/**餐馆服务员ID*/
	public final static String RESTAURANT_USER_ID = "userId";

	
	public final PreferenceDB SAVEPROPERTYDB_; // 控件存储属性集合的存储对象
	public final PreferenceDB permDB_; // 保存permanent user information invariant per session */
	public final PreferenceDB poiDB_; // 保存POI信息
	public final PreferenceDB shopCartDB_; // 保存购物车信息
	public boolean isInitStorage_ = false; // 是否已经初始化

	public PackageManagerAndroid(Activity app) throws Exception {
		permDB_ = new PreferenceDB(app, PERM_DB);
		poiDB_ = new PreferenceDB(app, POI_DB);
		SAVEPROPERTYDB_ = new PreferenceDB(app, SAVEPROPERTY_DB);
		shopCartDB_ = new PreferenceDB(app, SHOPCART_DB);
	}

	/**
	 * Generic method to read a string from preference database given a key.
	 */
	public String readString(PreferenceDB prefDB, String key) throws Exception {
		try {
			return prefDB.readAndDestroy(key);
		} finally {
		}
	}

	/**
	 * 在构造函数外分别初始化RMS，因此可以把该操作放在一个独立线程
	 */
	public void initStorage() throws Exception {
		isInitStorage_ = true;
		poiDB_.init();
	}

	/**
	 * 把字符串保存到数据库的通用方法
	 */
	void storeString(PreferenceDB prefDB, String key, String str) throws Exception {
		if (str != null) {
			try {
				prefDB.write(key, str);
			} finally {
			}
		}
	}
}
